package com.kejiguan.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.kejiguan.ui.adapter.DisplayMenuAdapter;
import com.kejiguan.ui.adapter.DisplaySettingItemAdapter;
import com.kejiguan.ui.dialog.AddNeItemDialog;
import com.kejiguan.ui.dialog.AddNeItemDialogUtil;
import com.kejiguan.ui.dialog.CommDialogUtil;
import com.kejiguan.ui.dialog.ModifyNeItemDialog;
import com.kejiguan.ui.dialog.ModifyNeItemDialogUtil;
import com.kejiguan.ui.event.RefreshNeStateEvent;
import com.kejiguan.ui.model.DisplayArea;
import com.kejiguan.ui.model.ExhibitionNeItem;
import com.kejiguan.ui.presenter.DisplaySettingFragmentPresenter;
import com.kejiguan.ui.presenter.QueryExStateEvent;
import com.kejiguan.ui.response.ExhibitionModifyResponse;
import com.kejiguan.ui.response.RemoveExhibitionResponse;
import com.kejiguan.ui.util.CacheDataUtil;
import com.kejiguan.ui.util.CommonDate;
import com.kejiguan.ui.widget.MyLinearLayoutManager;
import com.kejiguan.ui.widget.SpacesItemDecoration;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


/**
 * Created by apple on 16/3/13.
 */
public class DisplaySettingFragment extends Fragment implements View.OnClickListener,View.OnLongClickListener{

    private View rootView;
    private RecyclerView displayMenuList;
    private RecyclerView displayItemList;
    private Button addDeviceBtn;

    private int currentPosition=0;
    private DisplayMenuAdapter menuAdapter;
    private DisplaySettingItemAdapter adapter;

    private DisplaySettingFragmentPresenter mPresenter;
    private ExhibitionNeItem exhibitionNeItem;
    private ExhibitionNeItem delExhibitionNeItem;

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        EventBus.getDefault().register(this);
        mPresenter=new DisplaySettingFragmentPresenter();
        mPresenter.attach(getContext());
    }

    @Override
    public final View onCreateView(LayoutInflater inflater, ViewGroup container,
                                   Bundle savedInstanceState) {

        if (rootView == null) {

            rootView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_display_setting_layout, container, false);
            displayMenuList = (RecyclerView) rootView.findViewById(R.id.display_menu_list);
            displayItemList = (RecyclerView) rootView.findViewById(R.id.display_item_list);
            addDeviceBtn= (Button) rootView.findViewById(R.id.add_device_btn);


            addDeviceBtn.setOnClickListener(this);

            LinearLayoutManager displayAreaLayoutManager=new LinearLayoutManager(getContext());
            displayAreaLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            displayMenuList.setLayoutManager(displayAreaLayoutManager);
            menuAdapter=new DisplayMenuAdapter(getContext(), CacheDataUtil.getInstance().getDisplayAreas());
            menuAdapter.setOnClickListener(this);
            displayMenuList.setAdapter(menuAdapter);

            MyLinearLayoutManager gridLayoutManager=new MyLinearLayoutManager(getContext());
            gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            displayItemList.setLayoutManager(gridLayoutManager);
            displayItemList.addItemDecoration(new SpacesItemDecoration(40,false));
            adapter=new DisplaySettingItemAdapter(getContext(),CacheDataUtil.getInstance().getDisplayAreas().get(0).getExhibitionNeItems());
            adapter.setOnLongClickListener(this);
            adapter.setOnClickListener(this);
            displayItemList.setAdapter(adapter);
        }
        return rootView;
    }

    @Override
    public void onResume()
    {
        super.onResume();
        if (menuAdapter!=null)
        {
            menuAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.menu_name:
                int position= (int) v.getTag(R.id.recyclerview_item_position);
                currentPosition=position;
                menuAdapter.setSelectArea(position);
                adapter.setPresenterItems(CacheDataUtil.getInstance().getDisplayAreas().get(position).getExhibitionNeItems());
                break;
            case R.id.add_device_btn:
                Intent addExhibitionIntent=new Intent(getContext(),ActivationExhibitionActivity.class);
                addExhibitionIntent.putExtra("exhibitionAreaId",CacheDataUtil.getInstance().getDisplayAreas().get(currentPosition).getDisplayId());
                addExhibitionIntent.putExtra("exhibitionAreaName",CacheDataUtil.getInstance().getDisplayAreas().get(currentPosition).getDisplayName());
                startActivity(addExhibitionIntent);
                break;
            case R.id.neitem_item_layout:
                int positionTag= (int) v.getTag(R.id.recyclerview_item_position);
                Intent modifyExhibitionIntent=new Intent(getContext(),ModifyExhibitionActivity.class);
                modifyExhibitionIntent.putExtra("exhibition",CacheDataUtil.getInstance().getDisplayAreas().get(currentPosition).getExhibitionNeItems().get(positionTag));
                startActivity(modifyExhibitionIntent);
                break;
            case R.id.dialog_confirm_btn:
                CommDialogUtil.getInstance().dismiss();
                mPresenter.removeExhibitions(delExhibitionNeItem);
                break;
            case R.id.dialog_cancel_btn:
                CommDialogUtil.getInstance().dismiss();
                break;
        }


    }


    @Override
    public boolean onLongClick(View v) {
        switch (v.getId())
        {
            case R.id.neitem_item_layout:
                int neItemPosition= (int) v.getTag(R.id.recyclerview_item_position);
                delExhibitionNeItem =adapter.getNeItem(neItemPosition);
                if(!delExhibitionNeItem.getExhibitionState().equals(CommonDate.STATE_RUNNING))
                {
                    CommDialogUtil.getInstance().show(getContext(),"要移除"+ delExhibitionNeItem.getExhibitionName()+"展项吗?",DisplaySettingFragment.this,"移除","取消",0);
                }
                break;
        }
        return false;
    }



    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(RemoveExhibitionResponse response)
    {
        if ("0000".equals(response.getRetCode()))
        {
            for (DisplayArea displayArea: CacheDataUtil.getInstance().getDisplayAreas())
            {
                if (displayArea.getDisplayId().equals(response.getRemovedExhibitionNeItem().getExhibitionAreaId()))
                {
                    displayArea.getExhibitionNeItems().remove(response.getRemovedExhibitionNeItem());
                    EventBus.getDefault().post(new RefreshNeStateEvent());
                    EventBus.getDefault().post(new QueryExStateEvent());
                }
            }
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(ExhibitionModifyResponse response)
    {
        if ("0000".equals(response.getRetCode()))
        {
            adapter.notifyDataSetChanged();
            EventBus.getDefault().post(new QueryExStateEvent());
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(RefreshNeStateEvent event)
    {
        if (adapter!=null)
        {
            menuAdapter.notifyDataSetChanged();
            adapter.setPresenterItems(CacheDataUtil.getInstance().getDisplayAreas().get(currentPosition).getExhibitionNeItems());
        }

    }


}
